define(["angular",
    "framework/utils/JSONPrettyFormat",
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/msg/GillionMsgModule'
], function (angular, JSONPrettyFormat) {
    /**
     *  Module
     *
     * Description
     */
    var RuleAssemblyModule = angular.module('RuleAssemblyModule', ['DataGridModule',
        'GillionPaginationModule',
        'GillionMsgModule'
    ]);

    RuleAssemblyModule.controller('RuleAssemblyController', function ($scope, Resource, $dataSourceManager, GillionMsg, $location) {
        var ruleAssemblyResource = Resource("/grule-manager/ruleAssembly/:id", {
            id: "@id"
        });
        $scope.rulePackageId = $location.search().rulePackageId;
        $scope.search = {
            rulePackageId:  $scope.rulePackageId
        };

        Resource("/grule-manager/rulePackage/:id", {id: '@id'}).get({
            id: $scope.rulePackageId
        }, function (rulePackage) {
            $scope.rulePackage = rulePackage;
        });

        $scope.searchRuleAssembly = function () {
            $scope.search = $scope.search || {};
            var params = angular.extend($scope.search);
            $dataSourceManager.dataSources.ruleAssemblySource.doRequestData(1, params);
        }
        $scope.showPageView = function () {
            $scope.showPage = true;
            $scope.showAdd = false;
            $scope.showEdit = false;
        };
        $scope.showEditView = function () {
            $scope.showPage = false;
            $scope.showAdd = false;
            $scope.showEdit = true;
        };

        $scope.ruleAssemblyGridRendered = function (grid) {
            grid.actions.edit = function (row) {
                ruleAssemblyResource.get({
                    id: row.id
                }, function (result) {
                    delete result.$promise;
                    delete result.$resolved;
                    $scope.currId = result.id;
                    delete result.id;
                    if (result.conditions) {
                        result.conditions = JSON.parse(result.conditions);
                    }
                    $scope.editRuleAssembly = JSONPrettyFormat(result);
                    $scope.showEditView();
                });
            };
        };

        $scope.showAddView = function () {
            $scope.showPage = false;
            $scope.showAdd = true;
            $scope.showEdit = false;

            var templateRuleAssembly = {
                "name": "【必填】",
                "conditions": "【json格式】",
                "priority": "【整型】",
                "rulePackageId": $scope.rulePackageId,
                "remark": ""
            };

            $scope.addRuleAssembly = JSONPrettyFormat(templateRuleAssembly);
        };

        $scope.createRuleAssembly = function () {
            var ruleAssembly = JSON.parse($scope.addRuleAssembly);
            if (ruleAssembly.conditions) {
                ruleAssembly.conditions = JSON.stringify(ruleAssembly.conditions);
            }
            ruleAssemblyResource.create(ruleAssembly, function (response) {
                if (response.success) {
                    GillionMsg.alert("提示信息", "保存成功");
                    $scope.search = $scope.search || {};
                    var params = angular.extend($scope.search);
                    $dataSourceManager.dataSources.ruleAssemblySource.doRequestData(1, params);
                    $scope.showPageView();
                }else if (response.success !== false) {
                    GillionMsg.alert("提示消息", "保存失败，请核对保存信息！");
                }
            });
        };

        $scope.updateRuleAssembly = function () {
            var ruleAssembly = JSON.parse($scope.editRuleAssembly);
            if (ruleAssembly.conditions) {
                ruleAssembly.conditions = JSON.stringify(ruleAssembly.conditions);
            }
            ruleAssembly.id = $scope.currId;
            ruleAssemblyResource.update(ruleAssembly, function (response) {
                if (response.success) {
                    GillionMsg.alert("提示信息", "更新成功");
                    $scope.search = $scope.search || {};
                    var params = angular.extend($scope.search);
                    $dataSourceManager.dataSources.ruleAssemblySource.doRequestData(1, params);
                    $scope.showPageView();
                }else if (response.success !== false) {
                    GillionMsg.alert("提示消息", "更新失败，请核对更新信息！");
                }
            });
        };

        $scope.onRemoveRecord = function (event, row) {
            GillionMsg.confirm('提示信息', '是否确定删除？', function(r){
                if (r) {
                    ruleAssemblyResource.remove({
                        id: row.id
                    }, function (response) {
                        if (response.success) {
                            GillionMsg.alert("提示消息", "删除成功！");
                            $scope.searchRuleAssembly();
                        }
                    });
                }
            });
            return false;
        };

        $scope.showPageView();
    });
});